package edu.avans.hartigehap.web.controller;

import java.sql.Date;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import edu.avans.hartigehap.domain.WebCustomer;
import edu.avans.hartigehap.service.WebSecurityService;

@RequestMapping("/")
@Controller
public class WebSecurityController {

	@Autowired
	private WebSecurityService webSecurityService;

	@RequestMapping(value = {"/login"}, method = RequestMethod.POST)
	public String login(@RequestParam String username, @RequestParam String password, Model uiModel) {

		WebCustomer customer = webSecurityService.login(username, password);

		if(customer.getLoggedIn()){			
			uiModel.addAttribute("loggedIn", customer.getLoggedIn());
			uiModel.addAttribute("username", customer.getUsername());
			uiModel.addAttribute("password", customer.getPassword());
			System.out.println("LOGGED");
		}


		return"hartigehap/listrestaurants";
	}	

	@SuppressWarnings("deprecation")
	@PostConstruct
	public void createUserAccounts() {
		webSecurityService.createUserAccount("armando", "leyba", "Armando", "Leyba", new Date(1992,05,11), 
				"Leeuwerikstraat 11", "0652728510", "arleyba@avans.nl");
		webSecurityService.createAccount("maurits", "buijs", 
				"Molenweg 14", "0645789651", "maus@avans.nl");
	}
}
